import 'package:bigegg/data/repository/xmm_remote_repository.dart';
import 'package:bigegg/global/user/xmm_account_manager.dart';
import 'package:bigegg/utils/xmm_date_utils.dart';
import 'package:bigegg/utils/xmm_toast_util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../common/get/controller/xmm_base_controller.dart';
import '../../../common/log/xmm_log.dart';

class XmmUserEditController extends XmmBaseGetxController {

  XmmRemoteRepository remoteRepository = XmmRemoteRepository();

  void editAvatar(String path) async {
    try {
      EasyLoading.show();
      XmmLogManger().putLog(XmmLogType.clickEvent, {'page': XmmLogPages.pageEditAvatar});
      var userAvatar = await remoteRepository.updateUserAvatar(path);
      XmmAccountManager.instance.user.avatarUrl = userAvatar;
      XmmAccountManager.instance.updateUserInfo();
      XmmToastUtils.show('Update Successfully');
    } finally {
      EasyLoading.dismiss();
    }
  }

  void submitUserInfo(String nickname, DateTime birthday) async {
    try {
      EasyLoading.show();
      await remoteRepository.updateUserInfo(nickname, birthday);
      XmmAccountManager.instance.user.nickname = nickname;
      XmmAccountManager.instance.user.birthday = XmmDateUtils.parseTimeYYMMDD(birthday);
      XmmAccountManager.instance.updateUserInfo();
      XmmToastUtils.show('Edit Successfully');
      Get.back();
    } finally {
      EasyLoading.dismiss();
    }
  }
}
